<?php
/**
 * Created by PhpStorm.
 * User: longli
 * VX: isa1589518286
 * Date: 2021/07/16
 * Time: 17:33
 * @link http://www.lmterp.cn
 */

namespace app\common\model;

class OrdersPickInfo extends BaseModel
{
    protected $pk = 'info_id';

    // 设置 json 字段
    protected $json = ['goods'];

    // json 转为数组
    protected $jsonAssoc = true;

    /**
     * 关联拣货单
     * @return \think\model\relation\BelongsTo
     * @date 2021/07/16
     * @author longli
     */
    public function pick()
    {
        return $this->belongsTo(OrdersPick::class, 'pick_id', 'pick_id');
    }

    /**
     * 关联订单
     * @return OrdersPickInfo|\think\model\relation\HasOne
     * @date 2021/07/16
     * @author longli
     */
    public function order()
    {
        return $this->hasOne(Orders::class, 'order_id', 'order_id');
    }

    /**
     * 拣货单详情是否包含订单
     * @param Orders|int $order 订单或者订单 id
     * @return bool
     * @date 2021/07/17
     * @author longli
     */
    public static function hasOrder($order)
    {
        if($order instanceof Orders) $order = $order->order_id;
        return !!static::where("order_id", $order)->count();
    }

    /**
     * 更新面单打印状态
     * @return bool
     * @date 2021/07/20
     * @author longli
     */
    public function printLabel()
    {
        if(!\app\common\library\Tools::isCli())
            $this->print_user = session("lmterp")->id;
        $this->print_time = \app\common\library\Tools::now();
        return $this->save();
    }
}
